package com.example.service.impl;

import com.example.common.ErrorCode;
import com.example.exception.BusinessException;
import com.example.mapper.RoomMapper;
import com.example.model.PageBean;
import com.example.model.room.RoomAddRequest;
import com.example.model.room.RoomAllInfo;
import com.example.service.RoomService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class RoomServiceImpl implements RoomService {

    @Resource
    private RoomMapper roomMapper;

    @Override
    public Long add(RoomAddRequest roomAddRequest) {
        if (roomAddRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        if (roomAddRequest.getHotelId()==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"没有指定所属酒店");
        }
        return roomMapper.save(roomAddRequest);
    }

    @Override
    public Boolean update(RoomAddRequest roomInfo) {
        if (roomInfo.getHotelId()==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"没有指定所属酒店");
        }
        return roomMapper.update(roomInfo);
    }

    @Override
    public List<RoomAllInfo> getByHotelId(Long id) {
        return roomMapper.getByHotelId(id);
    }

    @Override
    public PageBean getRoomAll(Integer pageNumber, Integer pageSize,Long hotelId, Integer type, Integer minPrice, Integer maxPrice, String city) {
        PageHelper.startPage(pageNumber,pageSize);

        List<RoomAllInfo> roomAllInfos = roomMapper.getRoomAll(hotelId,type,minPrice,maxPrice,city);
        Page<RoomAllInfo> p= (Page<RoomAllInfo>) roomAllInfos;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public PageBean searchRooms(Long hotelId, Integer type, Integer minPrice, Integer maxPrice,Integer pageNumber,
                                Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);

        List<RoomAllInfo> roomAllInfos = roomMapper.searchRooms(hotelId, type, minPrice, maxPrice);
        Page<RoomAllInfo> p= (Page<RoomAllInfo>) roomAllInfos;

        return new PageBean(p.getTotal(),p.getResult());
    }

}
